#include "CServer.h"
#include "Logger.hpp"
#include "ConfigMgr.h"

int main()
{
	// 设置控制台代码页为 UTF-8
	SetConsoleCP(65001);	   // 设置控制台输入代码页
	SetConsoleOutputCP(65001); // 设置控制台输出代码页

	auto & CONFIG = ConfigMgr::Inst();
	std::string server_port_str;
	// 服务器端口号
	unsigned short server_port;
	server_port_str = CONFIG["Server"]["Port"];
	server_port = atoi(server_port_str.c_str());

	// server_port = 8081;
	// 初始化日志
	// auto LOG = Logger::GetInstance()->getLogger();

	try
	{
		LOG_INFO("Starting server...");
		// unsigned short port = static_cast<unsigned short>(8080);	//端口
		boost::asio::io_context ioc{1}; // 上下文
		boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
		signals.async_wait([&ioc](const boost::system::error_code &error, int signal_number)
						   {

			if (error) {
				return;
			}
			ioc.stop(); });
		std::make_shared<CServer>(ioc, server_port)->Start();
		// std::cout << "Server listen on port: " << server_port << std::endl;
		LOG_INFO("Server started!, listen on port: {}", server_port);
		ioc.run();
	}
	catch (std::exception const &e)
	{
		// std::cerr << "Error: " << e.what() << std::endl;
		LOG_ERROR("Error Occured!: {}", e.what());
		return EXIT_FAILURE;
	}
}